# 准备工作
import requests
import json
import pandas as pd

# 地理编码 => 函数形式
# 函数应用方案：在电话定位通讯中，给客户提供准确的地理信息。
def geocode(key, address, city=None, batch=None, ) -> str:
    """ 获取高德API的地理编码
       温馨提示：key是指高德api的密钥，请先去注册；
       address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo
    """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key": key,
        "address": address,
        "city": city,
        "batch": batch

    }
    response = requests.get(url, params=params)
    results = response.json()
    return results

# 逆地理编码
# 函数应用方案：数字的读取与储存比汉字容易，从而快速获取某些地理位置的名称。
def regeo(key,location,extensions="base",radius=None,):
    """"""
    url = 'https://restapi.amap.com/v3/geocode/regeo?parameters'
    params = {'key':key,
          'location':location,
          'extensions':extensions,
             'radius':radius
             }
    r = requests.get(url,params)
    results = r.json
    return results

# 步行路径规划
# 函数运用方案：运用到地图导航的路径规划中
# 数据准备：
origin = "广东省广州市中山大学南方学院"
destination = "广州市从化区龙岗"
# 思考：如果有1000个位置？重复代码？
# 函数！？ -->1、节省代码，重复利用  2、可以给小白（会一点python）用！
# 地理编码 =》 函数形式
# 函数一定不会报错，哪怕你写的语法有错误，哪怕你写的参数没定义？-----> 函数只有在执行（调用）时才会真正意义的运行！

def geocode(key,address,city=None,batch=None)->str:
    """获取高德API的地理编码
    注释：key是指高德API的秘钥，请先去注册
          address是指结构化地址，具体请查看：https://lbs.amap.com/api/webservice/guide/api/georegeo
    """
    url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address,
        "city":city,
        "batch":batch
    }
    response = requests.get(url,params=params)
    results = response.json()['geocodes'][0]['location']
    # 返回值
    return results

# 公交车路径规划
# 函数运用规划：运用到地图导航的路径规划中
def bus(key, origin, destination, city, cityd=None, extensions='base', strategy=0, nightflag=0, date=None, time=None,
        output='JSON'):
    """公交车的路径规划，orgigin与destination通过上面的geo函数获得"""
    url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "city": city,
        "output": "json"
    }
    response = requests.get(url, params=params)
    results = response.json()
    #     data = r.json()['route']['paths'][0]["steps"]
    #     for i in range (0,len(data)):
    #         print(data[int(i)]["instruction"])
    # 返回值
    return results

# 驾车路径规划
# 函数运用规划：运用到地图导航的路径规划中
def driving(key, origin, destination, extensions, originid=None, destinationid=None, strategy=0, origintype=None,
            destinationtype
            =None, output='JSON', waypoints=None, avoidpolygons=None, avoidroad=None, province=None, number=None,
            cartype=0,
            ferry=0, roadaggregation="false", callback=None):
    """驾车的路径规划，orgigin与destination通过上面的geo函数获得"""
    url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "extensions": extensions,
        "output": "json"
    }
    response = requests.get(url, params=params)
    results = response.json()
    #     data = r.json()['route']['paths'][0]["steps"]
    #     for i in range (0,len(data)):
    #         print(data[int(i)]["instruction"])
    # 返回值
    return results

# 骑行路径规划
# 函数调用规划：运用到地图导航的路径规划中
def bicycling(key, origin, destination):
    """骑行的路径规划，orgigin与destination通过上面的geo函数获得"""
    url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
        "key": key,
        "origin": origin,
        "destination": destination,
        "output": "json"
    }
    response = requests.get(url, params=params)
    results = response.json()
    # 返回值
    return results

# 货车路径规划
# 函数调用规划：运用到地图导航的路径规划中
def truck(key,origin,destination,size,originid=None,destinationid=None,strategy=1,origintype=None,destinationtype
         =None,output='JSON',diu=None,waypoints=None,height=None,width=None,load=None,weight=None,axis=None,province=None,
           number=None,cartype=0,avoidpolygons=None,nosteps=1):
    """驾车的路径规划，orgigin与destination通过上面的geo函数获得"""
    url = "https://restapi.amap.com/v4/direction/truck?parameters"
    params = {
    "key":key,
    "destination":destination,
    "origin":origin,
    "size":size,
    "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 行政区域查询
# 函数调用规划：用于用户输入个人信息时可自动查询用户输入的行政区域，节省时间
def district(key,keywords,subdistrict=1,page=1,offset=20,extensions='base',filter=None,callback=None,output="JSON"):
    """行政区域规划"""
    url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
    "key":key,
    "keywords":keywords,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 搜索POI
# 函数调用查询：可运用于城市宣传中，输入关键词即可查询出与之相关的信息
def search(key,keywords,types,city=None,citylimit='false',children=0,offset=20,page=1,extensions='base',callback=None,output="JSON"):
    """关键词搜索"""
    url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
    "key":key,
    "keywords":keywords,
     "types":types,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 周边搜索
# 函数调用规划：可运用于城市宣传中，输入地理编码即可查询出与之相关的信息
def searcharound(key,location,keywords=None,types=None,city=None,radius=3000,sortrule="distance",offset=20,page=1,extensions='base',callback=None,output="JSON"):
    """周边搜索"""
    url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
    "key":key,
    "location":location,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 多边形搜索
# 函数调用规划：可运用于城市宣传中，输入关键词即可查询出与之相关的信息
def polygon(key,polygon,keywords=None,types=None,offset=20,page=1,extensions='base',callback=None,output="JSON"):
    """多边形搜索"""
    url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
    "key":key,
    "polygon":polygon,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# ID搜索
# 函数调用规划：可运用于城市宣传中，输入ID即可查询出与之相关的信息
def searchID(key,id,callback=None,output="JSON"):
    """ID查询"""
    url = "https://restapi.amap.com/v3/place/detail?parameters"
    params = {
    "key":key,
    "id":id,
    "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# IP定位
# 函数调用规划：在用户通过网络做出违法犯罪行为时，可用IP定位找到嫌疑犯所在地
def ipconfig(key,ip,callback=None,output="JSON"):
    """ID查询"""
    url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
    "key":key,
    "ip":ip,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 静态地图
# 函数调用规划：用于商家展示自己店铺的地理位置
from PIL import Image
from io import BytesIO
import json


def staticmap(key, location, zoom, size=400 * 400, scale=1, markers=10, labels=10, paths=4, traffic=1, sig=None):
    """静态地图使用户能够将高德地图以图片形式嵌入自己的网页中。用户可以指定请求的地图位置、图片大小、以及在地图上添加覆盖物，如标签、标注、折线、多边形。"""
    url = "https://restapi.amap.com/v3/staticmap?parameters"
    params = {
        "key": key,
        "location": location,
        "zoom": zoom

    }
    r = requests.get(url, params=params)
    result = r.content
    results = Image.open(BytesIO(r.content))
    return results

# 坐标转换
# 函数调用规划：可以让不同的地理位置坐标进行转换，方便用户读取（在不同的软件中的不同格式可以相互转换）
def convert(key,locations,coordsys=None,output="JSON"):
    """坐标转换"""
    url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
    "key":key,
    "locations":locations,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 天气查询
# 函数调用规划：在小程序或软件中可以内嵌，增加软件多样性与可用性
def weatherinfo(key,city,extensions=None,output="JSON"):
    """天气查询"""
    url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
    "key":key,
    "city":city,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results

# 输入提示
# 函数调用规划：当用户输入关键词的前两位时可自动弹出输入提醒，节省了用户输入的时间，提高效率
def inputtips(key,keywords,type=None,location=None,city=None,citylimit="false",datatype="all",callback=None,output="JSON"):
    """输入提示，提供根据用户输入的关键词查询返回建议列表。"""
    url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
    "key":key,
    "keywords":keywords,
     "output":"json"
    }
    response = requests.get(url,params=params)
    results = response.json()
    # 返回值
    return results


